package com.dev.security.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.dev.security.entity.Menu;
import com.dev.security.entity.Role;
import com.dev.security.entity.RoleMenu;
import com.dev.security.entity.User;
import com.dev.security.mapper.RoleMapper;
import com.dev.security.mapper.RoleMenuMapper;
import com.dev.security.mapper.UserMapper;
import com.dev.security.service.RoleService;
import com.dev.security.service.UserService;
import com.dev.security.util.PasswordHash;
import com.dev.security.vo.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 角色Service实现
 *
 * @author yuboon
 * @version v1.0
 * @date 2020/04/04
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    @Transactional
    public int save(RoleVo roleVo) {
        Role roleEntity = new Role();
        // hutool
        BeanUtil.copyProperties(roleVo,roleEntity);
        roleEntity.setCreateDate(new Date());
        return roleMapper.insert(roleEntity);
    }

    @Override
    @Transactional
    public int update(RoleVo roleVo) {
        Role roleEntity = new Role();
        BeanUtil.copyProperties(roleVo,roleEntity);
        return roleMapper.update(roleEntity);
    }

    @Override
    @Transactional
    public int remove(int id) {
        return roleMapper.delete(id);
    }

    @Override
    public int batchRemove(String ids) {
        JSONArray jsonArray = JSONUtil.parseArray(ids);
        for(Object id : jsonArray){
            roleMapper.delete(Convert.toInt(id));
        }
        return jsonArray.size();
    }

    @Override
    public PageVo<RoleVo> selectList(RoleVo roleVo, PageVo pageVo) {
        if(StrUtil.isNotEmpty(roleVo.getCode())){
            roleVo.setCode(roleVo.getCode() + "%");
        }

        List<RoleVo> listVo = CollectionUtil.newArrayList();
        // 分页
        Page page = PageHelper.startPage(pageVo.getPage(), pageVo.getLimit());

        Role role = new Role();
        BeanUtil.copyProperties(roleVo,role);
        List<Role> listEntity = roleMapper.selectList(role);
        for(Role entity : listEntity){
            RoleVo vo = new RoleVo();
            BeanUtil.copyProperties(entity,vo);
            vo.setCreateDate(DateUtil.formatDate(entity.getCreateDate()));
            listVo.add(vo);
        }

        pageVo.setList(listVo);
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }

    @Override
    public int assignMenu(int roleId, String menuIds) {
        // 先删除
        roleMenuMapper.deleteByRoleId(roleId);

        // 后保存
        JSONArray jsonArray = JSONUtil.parseArray(menuIds);
        for(Object menuId : jsonArray){
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setMenuId(Convert.toInt(menuId));
            roleMenu.setRoleId(roleId);
            roleMenuMapper.insert(roleMenu);
        }

        return 0;
    }

    @Override
    public List<MenuVo> selectMenuByRoleId(int roleId) {
        List<Menu> menus = roleMenuMapper.selectMenuByRoleId(roleId);
        List<MenuVo> menuVos = CollectionUtil.newArrayList();

        for(Menu menu : menus){
            if(menu.getParentId() == 0){
                MenuVo treeVo = new MenuVo();
                treeVo.setId(menu.getId());
                menuVos.add(treeVo);
                buildTree(treeVo,menus,menuVos);
            }
        }

        return menuVos;
    }

    private void buildTree(MenuVo parentVo,List<Menu> list,List<MenuVo> menuVos){
        for(Menu menu : list){
            if(menu.getParentId() == parentVo.getId()){
                MenuVo childVo = new MenuVo();
                childVo.setId(menu.getId());
                // 父节点移除
                menuVos.remove(parentVo);
                menuVos.add(childVo);
                buildTree(childVo,list,menuVos);
            }
        }
    }

}
